<template>
  <div>
    <r-search
      :searchForm="searchForm"
      :searchHandle="searchHandle"
      :searchData="searchData"
    />
    <r-search :searchHandle="searchHandleAdd" />
    <r-table
      :isSelection="false"
      :isPagination="true"
      :tableData="tableData"
      :tableCols="tableCols"
      :tablePage="tablePage"
      @refresh="init()"
    ></r-table>
    <r-form
      :formRules="formRules"
      :formHandle="formHandle"
      :formData="formData"
      :formCols="formCols"
      ref="elForm"
      labelWidth="200px"
      dialogWidth="1000px"
      :isHandle="true"
    />
  </div>
</template>
<script>
export default {
  data() {
    return {
      searchData: {
        deviceNo: "",
        nickName: "",
        phone: "",
        deviceType: "",
        status: ""
      },
      searchForm: [
        {
          type: "input",
          prop: "deviceNo",
          placeholder: "设备编号",
          width: 150
        },
        {
          type: "input",
          prop: "nickName",
          placeholder: "报障联系人",
          width: 200
        },
        {
          type: "input",
          prop: "phone",
          placeholder: "报障联系电话",
          width: 200
        },
        {
          type: "select",
          prop: "deviceType",
          placeholder: "设备类型",
          width: 150,
          options: [
            { value: "1", label: "换电柜" },
            { value: "2", label: "租车" },
            { value: "3", label: "充电柜" },
            { value: "4", label: "电池" }
          ]
        },
        {
          type: "select",
          prop: "status",
          placeholder: "处理状态",
          width: 150,
          options: [
            {
              label: "未处理",
              value: 1
            },
            {
              label: "已处理",
              value: 2
            }
          ]
        }
        // {
        //   type: 'date',
        //   prop: 'adName',
        //   placeholder: '处理反馈时间',
        //   width: 150
        // },
      ],
      searchHandleAdd: [
        // {
        //   label: "添加广告",
        //   type: "success",
        //   handle: e => this.elFormVisible(),
        // },
      ],
      searchHandle: [
        {
          label: "搜索",
          type: "success",
          icon: "el-icon-search",
          handle: e => this.init()
        }
        // {
        //     label: '重置',
        //     icon: 'el-icon-refresh-right',
        //     handle: e => this.searchFromReset()
        // }
      ],
      tableData: [{}],
      tableCols: [
        {
          label: "序号",
          prop: "id"
        },
        {
          label: "设备编号",
          prop: "deviceNo"
        },
        {
          label: "报障人",
          prop: "nickName"
        },
        {
          label: "报障类型",
          prop: "faultReason"
        },
        {
          label: "故障描述",
          prop: "faultContent"
        },
        {
          label: "故障图片",
          prop: "faultImg",
          type: "images"
        },
        {
          label: "运维人员信息",
          prop: "maintenancerName"
        },
        {
          label: "处理状态",
          prop: "status",
          formatter: e =>
            e.status == 1 ? "未处理" : e.status == 2 ? "已处理" : ""
        },
        /* 	{
            label: '处理反馈',
            prop: 'dealDetails'
          }, */
        {
          label: "提交时间",
          prop: "stime"
        },
        {
          label: "处理反馈时间",
          prop: "ftime"
        }
        // {
        //   label: '操作',
        //   type: "button",
        //   width: 180,
        //   btnList: [
        //     {
        //       label: '去处理',
        //       type: 'primary',
        //       handle: (row) => this.elFormDetail(row),
        //     }
        //   ]
        // }
      ],
      tablePage: { limit: 10, page: 1, total: 0 },
      formData: {
        devType: "1",
        adName: "",
        url: "",
        equipmentNos: [],
        status: "",
        type: "1"
      },
      formCols: [
        {
          type: "input",
          label: "广告名称",
          width: 350,
          prop: "adName",
          rmblock: true,
          remark: "广告名称只是作为辨别多个广告条目之用，并不显示在广告中"
        },
        {
          type: "select",
          label: "选择设备",
          multiple: true,
          width: 350,
          prop: "equipmentNos",
          options: []
        },
        {
          type: "switch",
          label: "上线/下线",
          width: 350,
          prop: "status",
          values: [1, 2]
        }
      ],
      formRules: {
        adName: [
          { required: true, message: "请输入广告名称", trigger: "blur" }
        ],
        url: [{ required: true, message: "广告素材", trigger: "blur" }],

        equipmentNos: [
          { required: true, message: "选择设备", trigger: "blur" }
        ],
        status: [{ required: true, message: "在线状态", trigger: "blur" }]
      },
      formHandle: [
        {
          label: "取消",
          handle: e => this.elFormVisible()
        },
        {
          label: "确定",
          type: "success",
          handle: e => this.elFormSubmit()
        }
      ]
    };
  },
  components: {},
  computed: {},
  mounted() {
    this.init();
  },
  methods: {
    async init() {
      let parm = { ...this.tablePage, ...this.searchData };
      let res = await this.apis.fault_list(parm);
      if (res) {
        this.tablePage.total = res.total;
        var list = res.rows || [];
        this.tableData = list;
      }
      // this.getDevList()
    },
    async getDevList() {
      this.formCols[2].options = [];
      let res2 = await this.api.getAdvertList({ devType: 1 });
      if (res2) {
        for (let i = 0; i < res2.data.length; i++) {
          this.formCols[2].options.push({
            value: res2.data[i].equipmentNo,
            name: res2.data[i].name
          });
        }
        this.tableData.map(item => {
          // item.eq = []
          item.equipmentNos = item.equipmentNos.split(",");
          // for (let i = 0; i < item.equipmentNos.length; i++) {
          //     item.eq.push(this.formCols[2].options.filter(it => it.value == item.equipmentNos[i])[0])
          // }
        });
      }
    },
    async changeOnline(row) {
      Object.keys(this.formData).forEach(key => {
        this.formData[key] = row[key];
      });
      this.formData.id = row.id;
      this.formData.equipmentNos = this.formData.equipmentNos.join(",");
      console.log(this.formData.equipmentNos);
      await this.api.advertupdate(this.formData);
    },
    async elFormSubmit(row) {
      this.formData.equipmentNos = this.formData.equipmentNos.join(",");
      if (row) {
        await this.api.advertupdate(row);
      } else {
        let res = null;
        if (this.formData.id) {
          res = await this.api.advertupdate(this.formData); //advertsave
        } else {
          res = await this.api.advertsave(this.formData);
        }
        if (res) {
          this.elFormVisible();
          this.init();
        }
      }
    },
    elFormDetail(row) {
      Object.keys(this.formData).forEach(key => {
        this.formData[key] = row[key];
      });
      this.formData.id = row.id;
      // if (this.formCols[1].label == '登录密码') {
      //     this.formCols.splice(1, 1)
      // }

      this.elFormVisible();
    }
  }
};
</script>
<style scoped lang="scss">
.el-icon-search {
  margin-right: 10px;
}

.Tip {
  margin-left: 20px;
}
</style>
